package com.dreamer.repository.mobile;

import com.dreamer.domain.user.AccountsTransfer;
import com.dreamer.domain.user.User;
import com.wxjssdk.util.DateUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import ps.mx.otter.utils.SearchParameter;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by huangfei on 26/06/2017.
 */
@Repository
public class AccountsTransferDao extends BaseDaoImpl<AccountsTransfer> {

    private Session getCurrentSession() {
        return getSessionFactory().getCurrentSession();
    }

    public List<AccountsTransfer> findAccountsTransfer(SearchParameter<AccountsTransfer> parameter, User user) {
//        parameter.getEntity().setCanShow(true);
        Example example = Example.create(parameter.getEntity());
        DetachedCriteria dc = DetachedCriteria.forClass(AccountsTransfer.class);
        dc.add(example);
        if (!user.isAdmin()) {
            dc.add(Restrictions.or(Restrictions.eq("toAgent.id", user.getId()), Restrictions.eq("fromAgent.id", user.getId())));
        }
//        if (parameter.getEntity().getToAgent() != null) {
//            Agent agent =  parameter.getEntity().getToAgent();
//            //当事人
//            if(agent!=null){
//                DetachedCriteria dcAgent = dc.createCriteria("toAgent");
//                Example example1 = Example.create(agent).enableLike(MatchMode.ANYWHERE);
//                dcAgent.add(example1);
//            }
//        }
        addExample(dc, "toAgent", parameter.getEntity().getToAgent());
        addExample(dc, "fromAgent", parameter.getEntity().getFromAgent());
//        addExample(dc, "toAgent", parameter.getEntity().getToAgent());
//        addExample(dc, "fromAgent", parameter.getEntity().getFromAgent());
        dc.addOrder(Order.desc("id"));
        return searchByPage(parameter, dc);
    }

    /**
     * 批量存储记录
     * @param records
     */
    public void saveList(List<AccountsTransfer> records){
        for(int i=0;i<records.size();i++){
            AccountsTransfer record = records.get(i);
            getHibernateTemplate().save(record);
        }
    }

    /**
     * 获取奖励汇总
     * @param type
     */
    public List getMyReward(Integer agentId,Integer type,String startTime,String endTime){
        Date startDate = DateUtil.formatStr(startTime,"yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtil.formatStr(endTime,"yyyy-MM-dd HH:mm:ss");
        String hql="";
        if(1==type){
            hql="select t.fromAgent,t.toAgent,sum(t.amount) from AccountsTransfer t where t.fromAgent.id = :agentId and t.transferTime BETWEEN :startTime  and :endTime  GROUP BY t.fromAgent.id,t.toAgent.id";
        }

        if(2==type){
            hql="select t.fromAgent,t.toAgent,sum(t.amount) from AccountsTransfer t where t.toAgent.id = :agentId and t.transferTime BETWEEN :startTime  and :endTime  GROUP BY t.fromAgent.id,t.toAgent.id";
        }

        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("agentId",agentId);
        query.setParameter("startTime",startDate);
        query.setParameter("endTime",endDate);

        return query.list();

    }

    /**
     * 获取奖励明细
     * @param
     *
     */

    public List<AccountsTransfer> getRewardDetail(Integer fromId,Integer toId,String startTime,String endTime){
        DetachedCriteria dc = DetachedCriteria.forClass(AccountsTransfer.class);
        Date startDate = DateUtil.formatStr(startTime,"yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtil.formatStr(endTime,"yyyy-MM-dd HH:mm:ss");
        dc.add(Restrictions.and(Restrictions.eq("toAgent.id", toId), Restrictions.eq("fromAgent.id", fromId),
                Restrictions.between("transferTime", startDate,endDate)));
        dc.addOrder(Order.desc("transferTime"));
        List<AccountsTransfer> records = findByCriteria(dc, null, null);
        return records;
    }

}
